//
//  MerchantHTTPManager.m
//
//  Created by Dracy on 2019/12/25.
//  Copyright © 2019 Dracy. All rights reserved.

#import "DLProgressWebsViewController.h"
#import "DLJsBridgeBasePlugin.h"
#import <HBDNavigationBar/HBDNavigationBar.h>
#import <HBDNavigationBar/UIViewController+HBD.h>
#import <HBDNavigationBar/HBDNavigationController.h>
#import <MJRefresh/MJRefresh.h>
#import <Masonry/Masonry.h>

@interface DLProgressWebsViewController ()

@property (nonatomic, assign) Class handleClass;

@property (nonatomic, strong) DLJsBridgeBasePlugin *bridgeHangle;
@property (nonatomic, strong) CALayer *progresslayer;
@property (nonatomic, strong) CAGradientLayer *gradientLayer;
@property (nonatomic, strong) UIView *contentView;

@end

@implementation DLProgressWebsViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setContentViews];
}
- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    self.webView.scrollView.mj_header.hidden = YES;
    self.webView.scrollView.mj_header = nil;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if (self.isHideNav) {
        self.navigationController.navigationBar.hidden = NO;
    }
}

#pragma mark - setViews
-(void)dgg_layoutNavigation
{
    [super dgg_layoutNavigation];
    if (!self.navigationController.navigationBar.hidden &&self.navigationController.viewControllers.count>1) {
        [self resetNavigationBar];
    }
    if (self.isHideNav) {
        self.navigationController.navigationBar.hidden = YES;
    }
}

- (void)resetNavigationBar{
    [self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:CREATCOLORWITHSTRING(@"#F5F5F5")]];
    self.hbd_barShadowHidden = NO;
    if (self.isReseveNavigationBar) {
        kself
        [self.webView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(weakSelf.view).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
        }];
    }
}

- (void)dgg_setNav{
    [super dgg_setNav];
    //    // 导航栏颜色
    self.hbd_barTintColor = [UIColor whiteColor];
    // 导航栏两边按钮颜色
    self.hbd_tintColor = [UIColor colorWithHex:0x10BBB8];
    //    // 导航栏上标题颜色
    NSDictionary *attributes = [UINavigationBar appearance].titleTextAttributes;
    NSMutableDictionary *mutableAttributes = [attributes mutableCopy];
    [mutableAttributes addEntriesFromDictionary:@{ NSForegroundColorAttributeName: CREATCOLORWITHSTRING(@"##000000") }];
    self.hbd_titleTextAttributes = mutableAttributes;
    //    // 状态栏是 default 还是 lightContent
    self.hbd_blackBarStyle = YES;
    self.hbd_barShadowHidden = YES;
    //    [self wr_setStatusBarStyle:UIStatusBarStyleDefault];
    //    [self wr_setNavBarShadowImageHidden:YES];
    
}
- (void)setContentViews{
    self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, kNavHeight, SCREEN_WIDTH, 2)];
    [kApplicationWindow addSubview:self.contentView];
    [kApplicationWindow bringSubviewToFront:self.contentView];
    
    // Do any additional setup after loading the view.
    self.progresslayer = [[CALayer alloc]init];
    self.progresslayer.frame = CGRectMake(0, 0, 0, 2);
    self.progresslayer.backgroundColor = [UIColor colorWithHex:0x10BBB8].CGColor;
    
    //颜色渐变
    NSMutableArray *colors = [NSMutableArray arrayWithObjects:(id)[UIColor whiteColor].CGColor,(id)[UIColor colorWithHex:0x10BBB8].CGColor,nil];
    self.gradientLayer = [CAGradientLayer layer];
    //    gradientLayer.shadowPath = bezierPath.CGPath;
    self.gradientLayer.frame = CGRectMake(0, 0, 0, 2);
    self.gradientLayer.locations = @[@(0.0),@(1.0)];
    self.gradientLayer.startPoint = CGPointMake(0, 1);
    self.gradientLayer.endPoint = CGPointMake(1, 1);
    [self.gradientLayer setColors:[NSArray arrayWithArray:colors]];
    [self.progresslayer addSublayer:self.gradientLayer]; //设置颜色渐变
    [self.contentView.layer addSublayer:self.progresslayer];
}

#pragma mark - KVO
// 观察者
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    if ([keyPath isEqualToString:@"estimatedProgress"]) {
        self.progresslayer.opacity = 1;
        float floatNum = [[change objectForKey:@"new"] floatValue];
        self.progresslayer.frame = CGRectMake(0, 0, SCREEN_WIDTH*floatNum, 2);
        self.gradientLayer.frame = CGRectMake(0, 0, SCREEN_WIDTH*floatNum*0.5, 2);
        if (floatNum == 1) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                self.progresslayer.opacity = 0;
                
            });
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                self.progresslayer.frame = CGRectMake(0, 0, 0, 3);
            });
        }
        
    }else{
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
    
}
#pragma mark - webViewDelegate
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    NSLog(@"加载完成");
    [super webView:webView didFinishNavigation:navigation];
    //加载完成后隐藏progressView
    self.progresslayer.hidden = YES;
}

-(void)dealloc{
    
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
    [self.contentView removeFromSuperview];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
